package com.zhss.designpattern.factory;

public class FactoryMethodPatternDemo {

    public interface Product{
        public void execute();
    }

    public static class Product1 implements Product{
        public void execute() {
            System.out.println("----Product1 执行方法！---");
        }
    }

    public static class Product2 implements Product{
        public void execute() {
            System.out.println("----Product2 执行方法！---");
        }
    }

    abstract static class AbstractProductFactory{

         Product createProduct(){
            commonCreate();
            return specificCreate();
        }

        private void commonCreate(){
            System.out.println("---------共同执行的方法处理！！！！！--------");
        }

        abstract Product specificCreate();
    }

    public static class Product1Factory extends AbstractProductFactory{
        private static   Product1Factory instance = new Product1Factory();

        public static Product1Factory getInstance() {
            return instance;
        }

        @Override
        Product specificCreate() {
            return new Product1();
        }
    }

    public static class Product2Factory extends AbstractProductFactory{
        private static   Product2Factory instance = new Product2Factory();

        public static Product2Factory getInstance() {
            return instance;
        }

        @Override
        Product specificCreate() {
            return new Product2();
        }
    }

    public static void main(String[] args) {
        Product1 product1 = (Product1) Product1Factory.getInstance().createProduct();
        product1.execute();
        Product2 product2 = (Product2) Product2Factory.getInstance().createProduct();
        product2.execute();
    }
}
